setBrightness

abstract fun setBrightness(level: Int)

Attempts to set the screen brightness for the application's window or the device.

The specified level is an integer percentage, typically ranging from 0 (dimmest) to 100 (brightest). Values outside this range may be clamped to the nearest valid value by the system.

Important Considerations:

  • Device and OS Version Support: This functionality is not universally available on all Android devices. It generally requires Android 6.0 (API level 23) or higher. On unsupported devices or OS versions, this call may have no effect or could potentially throw an error if not handled gracefully by the underlying implementation.
  • Permissions: To modify system brightness settings, the application typically requires the android.permission.WRITE_SETTINGS permission. This is a special permission that must be explicitly granted by the user through the system settings screen; it cannot be granted at runtime via the standard permission request dialog. If the permission is not granted, this method will likely fail silently or throw a SecurityException, depending on the Android version and device.
  • User Override: The user can always override the screen brightness via system settings. Adaptive brightness, if enabled by the user, may also override programmatic changes.
  • Scope: This method might affect the brightness for the current application's window or, if WRITE_SETTINGS permission is granted, it might attempt to change the system-wide brightness. The exact behavior can vary.

It's recommended to check if brightness changes are effective and to guide users to system settings if direct programmatic control is not possible or insufficient.

Parameters

level

The desired brightness level, as an integer percentage from 0 (dimmest) to 100 (brightest).

See also

<a href="https://developer.android.com/reference/android/provider/Settings.System#SCREEN_BRIGHTNESS">

Settings.System.SCREEN_BRIGHTNESS

<a href="https://developer.android.com/reference/android/Manifest.permission#WRITE_SETTINGS">

Manifest.permission.WRITE_SETTINGS

<a href="https://developer.android.com/training/permissions/requesting#special-permissions">

Requesting Special Permissions in Android

For adjusting audio volume.